package collections;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;

/**
 * Created by pc on 2015/2/3.
 * 遍历过程中删集合中的元素可以吗* 
 * 用iterator时可以remove刚取出的元素，但是不能直接操作集合来remove*
 */
public class DeleteWhileIterate {
    public static void main(String args[]){
        Collection<String> books = new HashSet<String>();
        books.add("A");
        books.add("B");
        books.add("C");
        books.add("B");

        Iterator it = books.iterator();
        while (it.hasNext()){
            String temp = it.next().toString();
            System.out.println("ele: " + temp);
            if (temp.equals("B")) it.remove();
//            if (temp.equals("B")) books.remove(temp);
        }

        System.out.println(books.toString());
    }  
}
